import os
import numpy as np
import mmcv


def read_txt(file_path):
    lines = np.fromtxt(file_path)
    return lines


def _calculate_num_points_in_gt(data_path,
                                infos,
                                relative_path,
                                num_features=4):
    new_infos = []
    for info in mmcv.track_iter_progress(infos):
        pc_info = info['point_cloud']
        image_info = info['image']
        calib = info['calib']
        if relative_path:
            v_path = str(Path(data_path) / pc_info['velodyne_path'])
        else:
            v_path = pc_info['velodyne_path']
        points_v = np.fromfile(
            v_path, dtype=np.float32, count=-1).reshape([-1, num_features])
        rect = calib['R0_rect']
        Trv2c = calib['Tr_velo_to_cam']
        P2 = calib['P2']

        # points_v = points_v[points_v[:, 0] > 0]
        annos = info['annos']
        num_obj = len([n for n in annos['name'] if n != 'DontCare'])
        # annos = kitti.filter_kitti_anno(annos, ['DontCare'])
        # dims = annos['dimensions'][:num_obj]
        # loc = annos['location'][:num_obj]
        # rots = annos['rotation_y'][:num_obj]
        # gt_boxes_camera = np.concatenate([loc, dims, rots[..., np.newaxis]],
        #                                  axis=1)
        # gt_boxes_lidar = box_np_ops.box_camera_to_lidar(
        #     gt_boxes_camera, rect, Trv2c)
        gt_boxes_lidar = np.array(annos['gt_boxes_lidar'])
        indices = box_np_ops.points_in_rbbox(points_v[:, :3], gt_boxes_lidar)
        num_points_in_gt = indices.sum(0)
        num_ignored = len(annos['dimensions']) - num_obj
        num_points_in_gt = np.concatenate(
            [num_points_in_gt, -np.ones([num_ignored])])
        annos['num_points_in_gt'] = num_points_in_gt.astype(np.int32)
        new_info = info
        new_info['annos'] = annos
        new_infos.append(new_info)
    return new_infos


def filter_bbox_by_min_points(labels,infos, min_points_dict):
    for label in labels:
        name = label[0]
        if name in min_points_dict.keys():
            min_point = int(min_points_dict[name])
            


if __name__ == '__main__':
    pkl_file = "/cv/lyh/models/stereo-dsgn2/data/ww/ww_infos_train.pkl"
    import pickle as p
    contents = p.load(open(pkl_file, 'rb'))
    import pdb
    pdb.set_trace()
    print(contents.keys())
            